package com.example.ec.controller;

import com.example.ec.model.Goods;
import com.example.ec.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController extends  BaseController{
    private GoodsService goodsService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }
    @RequestMapping("signUp")
    private String signUp(Goods goods){
        goodsService.create("create",goods);
        session.setAttribute("goods",goodsService.queryAll());
        return "redirect:/admin/admin.jsp";
    }
    @GetMapping("queryById/{id}")
    private String queryById(@PathVariable("id") int id) {
        session.setAttribute("good", goodsService.queryById(id));
        return "redirect:/admin/edit.jsp";
    }
    @RequestMapping("modify")
    private String modify(Goods goods){
        goodsService.modify(goods);
        session.setAttribute("goods",goodsService.queryAll());
        return "redirect:/admin/admin.jsp";
    }
    @GetMapping("removeById/{id}")
    private String removeById(@PathVariable("id") int id){
        goodsService.removeById(id);
        session.setAttribute("goods",goodsService.queryAll());
        return "redirect:/admin/admin.jsp";
    }
    @RequestMapping("queryByName")
    private String queryByName(Goods goods){
       goods= goodsService.queryOne("queryByName",goods);
       if(goods!=null){
           session.setAttribute("goods",goods);
           return "redirect:/user/message.jsp";
       }
       session.setAttribute("error","your search goods is not exists...");
       return "redirect:/user/user.jsp";
    }
    @GetMapping("queryByNameGet/{name}")
    private  String queryByNameGet(@PathVariable("name") String name){
      Goods goods= (Goods) goodsService.query("queryByNameGet",name);
        session.setAttribute("goods",goods);
        return "redirect:/user/message.jsp";
    }

}
